﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XZL.GM.Bll;
using XZL.GM.Dal;
using XZL.GM.Entity;

public partial class Admin_Index : AdminBasePage
{
    List<Proc_MenuManage_SelectAllResult> menuList = new List<Proc_MenuManage_SelectAllResult>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblNickName.Text = AdminUser.NickName;
            GetAllMenuList();
            BindMenuList(0);
        }
    }

    /// <summary>
    /// 获取用户拥有的权限菜单项
    /// </summary>
    private void GetAllMenuList()
    {
        List<Proc_MenuManage_SelectAllResult> menuAll = UserInfoBiz.GetAllMenuList();

        if (AdminUser.UserName == "admin")
        {
            menuList = menuAll;
        }
        else
        {
            menuList = new List<Proc_MenuManage_SelectAllResult>();

            for (int i = 0; i < menuAll.Count; i++)
            {
                if (menuAll[i].ParentID == 0)
                {
                    menuList.Add(menuAll[i]);
                }
            }


            Proc_UserRole_SelectResult roleList = UserInfoBiz.GetUserRole(AdminUser.UserID);
            if (roleList != null)
            {
                List<string> list = new List<string>(roleList.MenuList.Split(','));
                for (int i = 0; i < list.Count; i++)   
                {
                    int roleID = Convert.ToInt32(list[i]);
                    for (int j = 0; j < menuAll.Count; j++)
                    {
                        if (roleID == menuAll[j].MenuID)
                        {
                            menuList.Add(menuAll[j]);
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// 获取子菜单 by[ParentID]
    /// </summary>
    /// <param name="pID"></param>
    /// <returns></returns>
    private List<Proc_MenuManage_SelectAllResult> GetMenuListByParentID(int pID)
    {
        List<Proc_MenuManage_SelectAllResult> ls = new List<Proc_MenuManage_SelectAllResult>();
        for (int i = 0; i < menuList.Count; i++)
        {
            if (menuList[i].ParentID == pID)
            {
                ls.Add(menuList[i]);
            }
        }
        return ls;
    }

    /// <summary>
    /// 绑定左侧菜单列表
    /// </summary>
    private void BindMenuList(int pID)
    {
        List<Proc_MenuManage_SelectAllResult> ls = GetMenuListByParentID(pID);
        for (int i = 0; i < ls.Count; i++)
        {
            if (pID == 0)
            {
                List<Proc_MenuManage_SelectAllResult> lsLower = GetMenuListByParentID(ls[i].MenuID);
                if (lsLower.Count > 0)
                {
                    litMenuList.Text += "<li><a href=\"" + ls[i].Url + "\" target=\"frameContent\" class=\"dropdown-toggle\"><i class=\"icon-desktop\"></i><span class=\"menu-text\"><b>" + ls[i].MenuName + " </b></span><b class=\"arrow icon-angle-down\"></b></a><ul class=\"submenu\">";
                    BindMenuList(ls[i].MenuID);
                    litMenuList.Text += "</ul></li>";
                }
            }
            else
            {
                litMenuList.Text += "<li class=\"liNav\"><a href=\"" + ls[i].Url + "\" target=\"frameContent\"><i class=\"icon-double-angle-right\"></i>" + ls[i].MenuName + "</a></li>";
            }
        }
    }
}